package com.comp490.level;

import java.util.ArrayList;
import java.util.List;

import com.comp490.shooter.GameView;
import com.comp490.sprite.EnemySprite;
import com.comp490.sprite.LargeEnemySprite;
import com.comp490.sprite.MediumEnemySprite;
import com.comp490.sprite.SmallEnemySprite;

public class LevelOne extends BaseLevel implements Level
{
    public LevelOne(GameView gameView)
    {
        super(gameView);
        totalNumberofWaves = 5;

        float x = gameView.getWidth() + 100;
        float y;
        List<EnemySprite> tempList = new ArrayList<EnemySprite>(6);
        for(int i = 0; i < 6; i++)
        {
            y = gameView.getHeight() * ((i + 1) / 6f);
            SmallEnemySprite ses = new SmallEnemySprite(gameView,
                    SmallEnemySprite.GREEN, i % 2, x, y);
            ses.setX(x);
            ses.setY(y);
            tempList.add(ses);
        }
        enemyWaveSet.put(0, new WaveStruct(5, tempList));

        tempList = new ArrayList<EnemySprite>(5);
        for(int i = 0; i < 5; i++)
        {
            y = gameView.getHeight() * ((i + 1) / 5f);
            SmallEnemySprite ses = new SmallEnemySprite(gameView,
                    SmallEnemySprite.BLUE, i % 2, x, y);
            ses.setX(x);
            ses.setY(y);
            tempList.add(ses);
        }
        enemyWaveSet.put(1, new WaveStruct(7, tempList));

        tempList = new ArrayList<EnemySprite>(8);
        for(int i = 0; i < 8; i++)
        {
            y = gameView.getHeight() * ((i + 1) / 13f);
            MediumEnemySprite ses = new MediumEnemySprite(gameView,
                    MediumEnemySprite.BLUE, i % 2, x, y);
            ses.setX(x);
            ses.setY(y);
            tempList.add(ses);
        }
        enemyWaveSet.put(2, new WaveStruct(6, tempList));

        tempList = new ArrayList<EnemySprite>(6);
        for(int i = 0; i < 6; i++)
        {
            y = gameView.getHeight() * ((i + 1) / 5f);
            SmallEnemySprite ses = new SmallEnemySprite(gameView,
                    SmallEnemySprite.RED, i % 2, x, y);
            ses.setX(x);
            ses.setY(y);
            tempList.add(ses);
        }
        enemyWaveSet.put(3, new WaveStruct(4, tempList));

        tempList = new ArrayList<EnemySprite>(8);
        for(int i = 0; i < 8; i++)
        {
            y = gameView.getHeight() * ((i + 1) / 13f);
            MediumEnemySprite ses = new MediumEnemySprite(gameView,
                    MediumEnemySprite.RED, i % 2, x, y);
            ses.setX(x);
            ses.setY(y);
            tempList.add(ses);
            if(i % 5 == 0)
            {
                SmallEnemySprite sm = new SmallEnemySprite(gameView,
                        SmallEnemySprite.YELLOW, i % 2, x, y);
                ses.setX(x);
                ses.setY(y);
                tempList.add(sm);
            }
        }

        enemyWaveSet.put(4, new WaveStruct(5, tempList));
    }

    private void beginWave()
    {
        if(++currentWave >= totalNumberofWaves)
            currentWave = 0;

        gameView.getEnemy_sprite_set().clear();
        WaveStruct wave = enemyWaveSet.get(currentWave);

        // Needs to check if one of the waves is done but others not
        if(wave == null || wave.enemyCycles == 0)
            return;

        if(!wave.isFirstPass())
        {
        	for(int i = 0; i < wave.getEnemySet().size(); i++)
        	{
        		wave.getEnemySet().get(i).setActive(true);
        		wave.getEnemySet().get(i).reset();
        	}
        }

        gameView.getEnemy_sprite_set().addAll(wave.getEnemySet());
        wave.decrementCycle();
    }

    public boolean timeInc()
    {
        // We are finished with all waves
        if(numOfWavesFinished == totalNumberofWaves)
        {
            if(!isSomeoneAlive() && !bossFight)
            {
                gameView.getEnemy_sprite_set().clear();
                List<EnemySprite> tempList = new ArrayList<EnemySprite>();
                LargeEnemySprite e = new LargeEnemySprite(gameView, 0,
                        EnemySprite.MOVE_STRAIGHT, gameView.getWidth() + 100,
                        gameView.getHeight() / 2);
                e.setX(gameView.getWidth() - e.getBitmapWidth() / 7);
                e.setY((gameView.getHeight() - e.getBitmapHeight()) / 2);
                tempList.add(e);
                enemyWaveSet.put(enemyWaveSet.size() - 1, new WaveStruct(1,
                        tempList));
                gameView.getEnemy_sprite_set().add(e);

                // done for isSomeoneAlive to check if boss is dead
                currentWave = enemyWaveSet.size() - 1;
                bossFight = true;
            }

            else if(!isSomeoneAlive() && bossFight)
            {
                gameView.getEnemy_sprite_set().clear();
                return true;
            }

            return false;
        }

        if(currentWave == -1 || !isSomeoneAlive())
            beginWave();
        return false;
    }
}
